<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../js/vue.js"></script>
</head>
<body>
    <div id="root">
        <!-- 功能实现：文字的透明度从有到无，周而复始 -->
        <h1 :style="{opacity: opacity}">你好呀,VUE</h1>
        <button @click="destoryChange">停止变化</button>
    </div>
    <script>
        Vue.config.productionTip = false;
        let vm = new Vue({
            el: '#root',
            data: {
                opacity:1,
            },
            methods: {
                destoryChange(){
                    // 手动销毁vm
                    this.$destroy();
                }
            },
            // vue将初始的真实DOM元素放到页面后（即挂载完毕），再执行mounted函数
            mounted() {
                this.timer = setInterval(() => {
                    vm.opacity -= 0.01;
                    if(vm.opacity <= 0) {
                        vm.opacity = 1;
                    }
                }, 16);
            },
            beforeDestroy() {
                // 清除定时器
                clearInterval(this.timer);
            },
        });
    </script>
</body>
</html>